package models

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"guanggao/appconfig"
)

type Guanggao struct {
	gorm.Model
	Name    string `gorm:"type:varchar(20);not null" json:"name"`
	Address string `gorm:"type:varchar(20);not null" json:"address"`
	Rq      string `gorm:"type:varchar(20);not null" json:"rq"`
	Img     string `gorm:"type:varchar(20);not null" json:"img"`
	Lj      string `gorm:"type:varchar(20);not null" json:"lj"`
	Px      string `gorm:"type:varchar(20);not null" json:"px"`
	Zd      string `gorm:"type:varchar(20);not null" json:"zd"`
	Zt      string `gorm:"type:varchar(20);not null" json:"zt"`
}

var DB *gorm.DB

func Init() {
	var err error
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	Mysqldata := appconfig.Appconf.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", Mysqldata.User, Mysqldata.Password, Mysqldata.Host, Mysqldata.Port, Mysqldata.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	DB.AutoMigrate(&Guanggao{})
}
func (g *Guanggao) List() (gg []*Guanggao, err error) {
	err = DB.Find(&gg).Error
	return gg, err
}
func (g *Guanggao) Add() error {
	return DB.Create(&g).Error
}
func (g *Guanggao) Update() error {
	return DB.Updates(&g).Error
}
func (g *Guanggao) Del() error {
	return DB.Delete(&g).Error
}
